package com.game.server.message.proto.execute;

import com.comm.ISession;
import com.game.majiang.config.ChexuanStructrue;
import com.game.majiang.config.DataManager;
import com.game.majiang.config.LogManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicRoom;
import com.game.majiang.logic.LogicServer;
import com.game.majiang.logic.LogicTable;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.message.proto.in.AbstractCreateRoomReq;
import com.game.server.message.proto.out.CreateRoomRsp;
import com.robot.RobotClient;
import com.robot.RobotManager;

public class CreateRoomReq extends AbstractCreateRoomReq {

  @Override
  public void Execute(ISession session) {
    LogManager.getInstance().Log("创建牌局的俱乐部id："+this.clubid);
    LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
    if (client != null && client.checkClick(msgcode)) {
      CreateRoomRsp rsp = new CreateRoomRsp();
      rsp.setIsOK((byte) 1);
      if (client.getTable() == null) {
        rsp.setIsOK((byte) 2);
        if (client.getVo().getIntegral() >= this.difen.intValue() * 50) {
          LogicRoom room = (LogicRoom) LogicServer.getInstance().getRoom(1);
          ChexuanStructrue struct = new ChexuanStructrue();
          struct.setDifen(this.difen);
          struct.setRoomname(this.roomname);
          struct.setDairumin(this.dairumin);
          struct.setShengmangbeishu(this.shengmangbeishu);
          struct.setAutoStartGameCount(this.autoStartGameCount);
          struct.setCanDairuTime(this.canDairuTime);
          struct.setXiumangorzoumang(this.xiumangorzoumang);
          struct.setKaiqimangquan(this.kaiqimangquan);
          struct.setGPSorIP(this.GPSorIP);
          struct.setDijiuwang(this.dijiuwang);
          struct.setFanghuopai(this.fanghuopai);
          struct.setGameallTime(this.gameallTime);
          struct.setGamesPeopleCount(this.gameallCount);
          struct.setBankerid(client.getUserId());

          struct.setControlBringIn(this.controlBringIn);
          LogicTable createTable = room.createTable(struct);
          createTable.enterWatchPlayer(client);
          client.setTableid(createTable.getID());
          client.setRoomid(room.getID());
          createTable.setClubid(this.clubid);
          XyClubInfo xyClubInfo = DataManager.getInstance().getMapClub().get(this.clubid);
          LogManager.getInstance().Log("xyClubInfo：" + xyClubInfo);
          if (xyClubInfo != null) {
            LogManager.getInstance().Log("Unionid：" + xyClubInfo.getUnionid());
            createTable.setUnionid(xyClubInfo.getUnionid());
          }

          rsp.setIsOK((byte) 0);
          client.getVo().AddIntegral(-(this.difen.intValue() * 50)); //扣费
          client.getVo().Save();
        }
      }
      client.Send(rsp);

      // if (rsp.getIsOK().intValue() == 0 && this.clubid == 0) {
      //   LogicTable table = (LogicTable) client.getTable();
      //   LogManager.getInstance().Log("this.clubid" + table);
      //   // 添加观战机器人
      //   for (int i = 0; i < 1; i ++) {
      //     RobotClient createRobot = RobotManager.getInstance().createRobot(table);
      //     if (createRobot == null) continue;
      //     if (createRobot.getClient() != null && LogicServer.getInstance().getClient(createRobot.getClient().getUserId()) != null) {
      //       createRobot.getClient().setTableid(table.getID());
      //       createRobot.getClient().setRoomid(1);
      //       table.enterWatchPlayer(createRobot.getClient()); //进入观战
      //       table.toTableInfo(createRobot.getClient());     //坐下
      //       RobotManager.getInstance().addRobot(createRobot);
      //     }
      //   }
      // }
    }
  }

}
